package com.nwpusp.transmitter.net;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Vector;

import android.util.Log;

import com.nwpusp.controller.actions.*;
import com.nwpusp.model.session.ConnectionId;

public class AsServer_SendToClient extends Vector<Socket>{

	private static final long serialVersionUID = 1L;
	
	private LinkedHashMap<Socket, Send2ClientThread> sock2TrdHashMap = new LinkedHashMap<Socket, Send2ClientThread>();
	
	private class Send2ClientThread extends Thread {
		ObjectOutputStream objOutputStream = null;
		Socket socket = null;
		ArrayList<Action> al = new ArrayList<Action>();
		boolean isExit = false;
		
		public Send2ClientThread(Socket socket) {
			this.socket = socket;
			Init(socket);
		}
		
		public void Init(Socket sock) {
			al.clear();
			if(objOutputStream == null) {
				try {
					objOutputStream = new ObjectOutputStream(sock.getOutputStream());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
		synchronized public void setExit(boolean exit) {
			 isExit = exit;
			 notifyAll();
		}
		
		public void run() {
			while(!isExit) {
				 ArrayList<Action> lal = fetchActionList();
				 if(!lal.isEmpty()) {
					 try {
						Log.d("JustinAlNum", "lal: " + String.valueOf(lal.size()));
						objOutputStream.writeObject(lal);
						objOutputStream.flush();
					} catch (IOException e) {
						e.printStackTrace();
					}
				 }
			}
			try {
				if(socket != null) {
					socket.close();
					socket = null;
				}
				if(objOutputStream != null) {
					objOutputStream.close();
					objOutputStream = null;
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		synchronized public void appendAction(ArrayList<Action> ial) {
			if(!ial.isEmpty()) {
				this.al.addAll(ial);
				notifyAll();
			}
		}
		
		synchronized public ArrayList<Action> fetchActionList() {
			if(al.isEmpty()) {
				try {
					wait();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			ArrayList<Action> ral = new ArrayList<Action>();
			ral.addAll(al);
			al.clear();
			return ral;
		}
	}
	
	
	public void remove(Socket sock){
		Send2ClientThread trd = sock2TrdHashMap.get(sock);
		trd.setExit(true);
		try {
			trd.join();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		sock2TrdHashMap.remove(sock);
		super.remove(sock);
	}
	
	public boolean add(Socket sock) {
		boolean ret = super.add(sock);
		Send2ClientThread trd = new Send2ClientThread(sock);
		sock2TrdHashMap.put(sock, trd);
		trd.start();
		return ret;
	}
	
	synchronized void sendToAll(ArrayList<Action> al) {
		Log.d("JustinBroadCast", "I have broadcast!");
		Socket sock;
		for(int i = 0; i < size(); i++) {
			sock = (Socket)elementAt(i);
			Send2ClientThread trd = sock2TrdHashMap.get(sock);
			trd.appendAction(al);
		}
	}
	
	synchronized void sendToONE(Socket socket, ArrayList<Action> al){
		Log.d("JustinAlNum", String.valueOf(al.size()));
		Socket sock;
		for(int i = 0; i < size(); i++){
			sock = (Socket)elementAt(i);
			if(socket == sock) {
				Send2ClientThread trd = sock2TrdHashMap.get(sock);
				trd.appendAction(al);
			}
		}
	}
	
}
